﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SARD.Aux_Forms
{
    public partial class equipamentonovo : Form
    {


        string sfile;
        bool newequipment;
        string dirpath;

        /// <summary>
        /// This form stores the equipment information's inside a proper file.
        /// </summary>
        /// <param name="path">
        /// It's the path to the equipment's folder. 
        /// </param>
        /// <param name="newequipment">
        /// It's a boolean valiable describing if it already exists or not.
        /// </param>
        public equipamentonovo(string path, bool newequipment)
        {
            InitializeComponent();
            comboBox1.SelectedIndex = 1;
            dirpath = path;

            if (newequipment)
            {
                this.newequipment = true;
            }

            else
            {
                string[] file = Directory.GetFiles(path, "*.txt");
                sfile = file[0];
                string[] text = File.ReadAllLines(sfile);
                textBox1.Text = text[0].Substring("#Equipamento:".Length + 1);
                comboBox1.Text = text[1].Substring("#Tipo:".Length + 1);
                textBox3.Text = text[2].Substring("#Localização:".Length + 1);
                textBox4.Text = text[3].Substring("#Número de Série:".Length + 1);
                textBox5.Text = text[4].Substring("#Observações:".Length + 1);
            }



        }


        /// <summary>
        /// This function closes the form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        /// <summary>
        /// this function closes the form, but before it happens the data will be saved.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (newequipment)
            {
                Directory.CreateDirectory(dirpath + "\\" + textBox1.Text.ToString());
                StreamWriter place = new StreamWriter(dirpath + "\\" + textBox1.Text.ToString() + "\\" + textBox1.Text.ToString() + ".txt");

                place.WriteLine("#Equipamento: " + textBox1.Text.ToString());
                place.WriteLine("#Tipo: " +comboBox1.Text.ToString());
                place.WriteLine("#Localização: " + textBox3.Text.ToString());
                place.WriteLine("#Número de Série: " + textBox4.Text.ToString());
                place.WriteLine("#Observações: " + textBox5.Text.ToString());
                place.WriteLine();
                place.Close();

            }
            else
            {
                string[] fulltext = File.ReadAllLines(sfile);

                fulltext[0] = "#Equipamento: " + textBox1.Text.ToString();
                fulltext[1] = "#Tipo: " + comboBox1.Text.ToString();
                fulltext[2] = "#Localização: " + textBox3.Text.ToString();
                fulltext[3] = "#Número de Série: " + textBox4.Text.ToString();
                fulltext[4] = "#Observações: " + textBox5.Text.ToString();

                File.WriteAllLines(sfile, fulltext);

                //Due to Windows restrictions it is necessary to check if the file or folder already exists before creating it.
                if (sfile != dirpath + "\\" + textBox1.Text.ToString() + ".txt")
                    File.Move(sfile, dirpath + "\\" + textBox1.Text.ToString() + ".txt");

                if (dirpath != Directory.GetParent(dirpath).FullName + "\\" + textBox1.Text.ToString())
                    Directory.Move(dirpath, Directory.GetParent(dirpath).FullName + "\\" + textBox1.Text.ToString());

            }

            this.Close();

            // at this position I need to send the position and the file containing this new device to the analisys function.
            this.Close();
        }


        /// <summary>
        /// The following functions will swap between the textbox and buttons on the graphic interface.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                comboBox1.Focus();
                e.Handled = true;
            }


        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                textBox3.Focus();
                e.Handled = true;
            }

            if (e.KeyCode == Keys.Up)
            {
                textBox1.Focus();
                e.Handled = true;
            }
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                textBox4.Focus();
                e.Handled = true;
            }

            if (e.KeyCode == Keys.Up)
            {
                comboBox1.Focus();
                e.Handled = true;
            }
        }

        private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                textBox5.Focus();
                e.Handled = true;
            }

            if (e.KeyCode == Keys.Up)
            {
                textBox3.Focus();
                e.Handled = true;
            }
        }

        private void textBox5_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                button2.Focus();
                e.Handled = true;
            }

            if (e.KeyCode == Keys.Up)
            {
                textBox4.Focus();
                e.Handled = true;
            }
        }
    }
}
